<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

class OnlineAsk extends AdminBase
{
    public function saveMessage(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $data['create_time'] = time();
            $result = Db::name('message')->strict(false)->insert($data);
            if($result){
                return json(['status' => 1 , 'msg' => 'ok']);
            }else{
                return json(['status' => -1 , 'error' => 'save error']);
            }
        }
    }

    public function showMessageList($id){
        $list = Db::name('message')->where('mid',$id)->select();
        if($list){
            return json(['status' => 1 , 'msg' => 'ok' , 'list' => $list]);
        }else{
            return json(['status' => -1 , 'error' => 'get message failed']);
        }
    }

    public function createMessage(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if(isset($data['id'])) unset($data['id']);
            $data['create_time'] = time();
            $result = Db::name('message_create')->strict(false)->insert($data);
            if($result){
                return json(['status' => 1 , 'msg' => 'ok']);
            }else{
                return json(['status' => -1 , 'error' => 'save error']);
            }
        }
    }

    public function updateMessage($id){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = Db::name('message_create')->strict(false)->where('id',$id)->update($data);
            if($result){
                return json(['status' => 1 , 'msg' => 'ok']);
            }else{
                return json(['status' => -1 , 'error' => 'save error']);
            }
        }
    }

    public function LinkDetail($id){
        $list = Db::name('message_create')->where('id',$id)->find();
        $list['speech'] = html_entity_decode($list['speech']);
        if($list){
            return json(['status' => 1 , 'msg' => 'ok' , 'list' => $list]);
        }else{
            return json(['status' => -1 , 'error' => 'get message failed']);
        }
    }

    public function LinkList(){
        $list = Db::name('message_create')->select();
        if($list){
            return json(['status' => 1 , 'msg' => 'ok' , 'list' => $list]);
        }else{
            return json(['status' => -1 , 'error' => 'get message failed']);
        }
    }

    public function unifiedUpload($name){
        $file = $this->request->file($name);
        if($file){
            $data = new Upload();
            $result = $data->unifiedUpload($file);
            return json($result);
        }
        return '错误';
    }
}